package cn.tyf.service.imp;

import cn.tyf.dao.NoticeDao;
import cn.tyf.domain.Notice;
import cn.tyf.domain.PageBean;
import cn.tyf.domain.Record;
import cn.tyf.service.NoticeService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Component
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeDao noticeDao;

    @Override
    public void addNotice(Notice notice) {
        noticeDao.addNotice(notice);
    }

    @Autowired
    private PageBean<Notice> pageBean;

    @Override
    public PageBean<Notice> findByPage(Integer currentPage, Integer pageSize) {
        //1总记录数
        Integer count = noticeDao.findCount();
        pageBean.setTotalCount(count);
        //总页数
        Integer totalPage = (count % pageSize==0) ? count / pageSize : count / pageSize +1;
        pageBean.setTotalPage(totalPage);
        //当前页码
        pageBean.setCurrentPage(currentPage);
        //每页记录数
        pageBean.setPageSize(pageSize);
        //数据
        int start = (currentPage - 1) * pageSize;

        List<Notice> noticePages = noticeDao.findByPage(start, pageSize);

        pageBean.setList(noticePages);
        return pageBean;
    }

    @Override
    public Notice findByNotice(Integer nid) {
        return noticeDao.findByNotice(nid);
    }

    @Override
    public void updateNotice(Notice notice) {
        notice.setTime(new DateTime().toString("yyyy-MM-dd HH-mm-ss"));
        noticeDao.updateNotice(notice);
    }

    @Override
    public void deleteByNid(Integer nid) {
        noticeDao.deleteByNid(nid);
    }

    @Override
    public List<Notice> findAll() {
        return noticeDao.finAll();
    }
}
